package com.banyuan.课堂练习.IO流;

import java.io.*;

/*
 * 将一个文件分割成两份，分别存入两个文件中；
 *用字节流解决；
 *  */
public class Ex3Plus {
    public static void main(String[] args) {
        try (BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream("aaa.txt"));
             BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream("aaa.txt"));
             BufferedOutputStream bos1 = new BufferedOutputStream(new FileOutputStream("aaa1.txt"));
             BufferedOutputStream bos2 = new BufferedOutputStream(new FileOutputStream("aaa2.txt"))
        ) {
            int avg = bis1.available() / 2;
            int rest = avg;
            byte[] arr = new byte[8];
            while (rest > 0) {
                int len = bis1.read(arr);
                //这边作出一个比较，将len和rest中较小的一个数传给长度；
                bos1.write(arr, 0, Math.min(len, rest));
                bos1.flush();
                rest -= 8;
            }
            bis2.skip(avg);
            int len;
            while ((len = bis2.read(arr)) != -1) {
                bos2.write(arr, 0, len);
                bos2.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
